<template>
  <div class="app-container">
    <el-card>
      <el-form :model="list_q" ref="list_q_ref" inline>
        <el-form-item
          v-for="(
            { form_item_prop: { prop, label }, component_name, component_prop },
            index
          ) in list_q_c"
          :key="index"
          v-bind="{ prop, label }"
        >
          <component
            :is="component_name"
            v-bind="component_prop"
            v-model="list_q[prop]"
            :placeholder="label"
          >
            <template v-if="component_name == 'el-select'">
              <el-option
                v-for="i in component_prop.option_data"
                :key="i.id"
                :value="i[component_prop.option_prop.value]"
                :label="i[component_prop.option_prop.label]"
              ></el-option>
            </template>
          </component>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="get_list">搜索</el-button>
          <el-button type="primary" @click="reset_list_q">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <el-table :data="list">
        <el-table-column
          v-for="item in list_c"
          :key="item.prop"
          v-bind="item"
        ></el-table-column>
        <el-table-column label="操作" v-slot="{ row }">
          <el-button
            :disabled="row.approveStatus == '通过'"
            type="primary"
            size="mini"
            @click="detail_approve(row)"
            >详情</el-button
          >
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="
          (val) => {
            pager.limit = val;
            get_list();
          }
        "
        @current-change="
          (val) => {
            pager.page = val;
            get_list();
          }
        "
        :current-page="pager.page"
        :page-sizes="[2, 10, 20, 30]"
        :page-size="pager.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pager.total"
        style="text-align: right; margin-top: 20px"
      >
      </el-pagination>
    </el-card>
    <el-dialog
      title="审核销售订单"
      @close="visible = false"
      :visible.sync="visible"
      center
    >
      <el-form>
        <el-form-item label="审核意见">
          <el-input
            type="textarea"
            v-model="list_e.approveAdvice"
            :rows="4"
            resize="none"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="visible = false">取消</el-button>
        <el-button @click="approve('通过')" type="primary">通过</el-button>
        <el-button @click="approve('驳回')" type="danger">驳回</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getApplyApproveUserList } from '@/api_zc/task/sale-order-approve'
import { getPurchaseRequestList, PurchaseRequestApprove } from '@/api_zc/scm/purchase-management/purchase-request'
import { list_q_c, list_c, list_q } from './config'
export default {
  mounted () {
    this.get_list()
    this.get_unity_list()
  },
  data () {
    return {
      list: [],
      list_q,
      list_q_c,
      list_c,
      list_e: {
        approveStatus: null,
        id: null,
        approveAdvice: null
      },
      pager: {
        page: 1,
        limit: 10,
        total: 0
      },
      visible: false
    }
  },
  methods: {
    async get_list () {
      if (!this.list_q.applyAttr) {
        return
      }
      if (!this.list_q.approveAttr) {
        return
      }
      [this.list_q.applyStartTime, this.list_q.applyEndTime] = this.list_q.applyAttr;
      [this.list_q.approveStartTime, this.list_q.approveEndTime] = this.list_q.approveAttr
      const { data: { total, content } } = await getPurchasePlanList({ ...this.list_q, ...this.pager })
      this.list = content
      this.pager.total = total
    },
    async get_unity_list () {
      const { data: data2 } = await getApplyApproveUserList()
      this.list_q_c.find(item => item.form_item_prop.label == '申请人').component_prop.option_data = data2
      this.list_q_c.find(item => item.form_item_prop.label == '审核人').component_prop.option_data = data2
    },
    detail_approve (row) {
      Object.assign(this.list_e, row)
      this.visible = true
    },
    reset_list_q () {
      this.$refs.list_q_ref.resetFields()
      this.get_list()
    },
    async approve (flag) {
      this.list_e.approveStatus = flag
      await PurchasePlanApprove(this.list_e)
      this.visible = false
      this.get_list()
    }
  }
}
</script>

<style>
</style>